﻿//提交表单是检查所有数据是否合法
function checkForm(){
	if(checkUserName()&&checkPass()&&checkEmail()&&checkYear()){
		return true;
	}else{
		alert("提交的数据有误，请重新输入！");
		return false;
	}
}

//检查用户名是否合法
function checkUserName(){
	var name = document.getElementById("username").value;
	var regName = /^\S{4,20}$/;
	if(regName.test(name) == false){
		document.getElementById("usernameMessage").className = "c_red";
		document.getElementById("usernameMessage").innerHTML = "格式不正确，用户名必须4-16个字符";
		return false;
	} else {
		document.getElementById("usernameMessage").className = "c_green";
		document.getElementById("usernameMessage").innerHTML = "输入正确";
		return true;
	}

}

//检查密码是否合法
function checkPass(){
	var pwd = document.getElementById("password").value;
	var pwd2 = document.getElementById("repassword").value;
	//验证密码
	if(pwd==""){
		document.getElementById("passwordMessage").className = "c_red";
		document.getElementById("passwordMessage").innerHTML = "密码不能为空";
		return false;
	}else if(pwd.length<6||pwd.length>12){
		document.getElementById("passwordMessage").className = "c_red";
		document.getElementById("passwordMessage").innerHTML = "密码长度为6-12个字符";
		return false;
	} else {
		document.getElementById("passwordMessage").className = "c_green";
		document.getElementById("passwordMessage").innerHTML = "输入正确";
	}
	//验证确认密码
	if(pwd2!=pwd){
		document.getElementById("repasswordMessage").className = "c_red";
		document.getElementById("repasswordMessage").innerHTML = "确认密码与密码输入不一致";
		return false;
	}
	document.getElementById("repasswordMessage").className = "c_green";
	document.getElementById("repasswordMessage").innerHTML = "输入正确";
	return true;
}

//检查邮箱是否合法
function checkEmail(){
	var email = document.getElementById("email").value;
	var regEmail = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	if(regEmail.test(email) == false){
		document.getElementById("emailMessage").className = "c_red";
		document.getElementById("emailMessage").innerHTML = "邮箱格式不正确";
		return false;
	} else {
		document.getElementById("emailMessage").className = "c_green";
		document.getElementById("emailMessage").innerHTML = "输入正确";
		return true;
	}
}

//检查年份是否合法
function checkYear(){
	var year = document.getElementById("year").value;
	var now=new Date();
	if(year==""){
		document.getElementById("birthdayMessage").className = "c_red";
		document.getElementById("birthdayMessage").innerHTML = "请输入出生年份";
		return false;
	}else if(isNaN(year)){
		document.getElementById("birthdayMessage").className = "c_red";
		document.getElementById("birthdayMessage").innerHTML = "年份必须是数字";
		return false;
	}else if(parseInt(year)<1949||parseInt(year)>now.getFullYear()){
		document.getElementById("birthdayMessage").className = "c_red";
		document.getElementById("birthdayMessage").innerHTML = "年份范围从1949-"+now.getFullYear()+"年";
		return false;
	} else {
		document.getElementById("birthdayMessage").className = "c_green";
		document.getElementById("birthdayMessage").innerHTML = "输入正确";
		return true;
	}
}

//定义月份列表和日期列表
var monthList = new Array();
monthList = ['一','二','三','四','五','六','七','八','九','十','十一','十二'];
var dayList = new Array();
dayList = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31'];

//选择月份时绑定日期 
function changeMonth(){
	var year = Number(document.getElementById("year").value);
	var flag = false;
	if(year%400==0 || (year%4==0 && year%100!=0)){
		flag = true;
	}
	var month= document.getElementById("month"); 
	var index = month.selectedIndex;
	var selectMonth = month[index].text;
	var day= document.getElementById("day");
	day.options.length=0;
	for(var i=0; i<monthList.length; i++){
		if (monthList[i] == selectMonth){
			for (var j=0; j<dayList.length; j++){
				if(selectMonth=="二" && flag == false && dayList[j] == "29"){
					break;
				}
				if(selectMonth=="二" && flag == true && dayList[j] == "30"){
					break;
				}
				if((selectMonth=="四" || selectMonth=="六" || selectMonth=="九" || selectMonth=="十一")&&(dayList[j] == "31")){
					break;
				}
				try{
					day.add(new Option(dayList[j],dayList[j]),null);
				}catch(e){
					day.add(new Option(dayList[j],dayList[j]));
				}
			}
		}
	}
}

//绑定月份
function allMonth(){
	var month= document.getElementById("month");
	var value = 1;
    for (var i=0; i<monthList.length; i++){
		try{
			month.add(new Option(monthList[i], value),null); //标准方式
		} catch(e){
			month.add(new Option(monthList[i], value));  //IE
		}
		value ++;
	}
	changeMonth();
}

function picText(){
	var image = document.getElementById("pic");
	var src;
	if(image) {
		if (window.navigator.userAgent.indexOf("MSIE")>=1){ // ie
			image.select();
			src = document.selection.createRange().text;
		}else if(window.navigator.userAgent.indexOf("Firefox")>=1){  // firefox
			if(image.files){
				src = image.files.item(0).getAsDataURL();
			}
		}
	}
	document.getElementById("image").src = src;
}

//加载时绑定月份
window.onload=allMonth;